package com.recharge.web.controller;

import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.RegionQueryDO;
import com.recharge.domain.vo.RegionDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.RegionService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/region")
public class RegionController extends BaseController{

    @Autowired
    private RegionService RegionService;

    @RequiresPermissions("region:add")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    @ApiOperation(value="添加城市", notes="")
    public @ResponseBody
    ObjectResponse<RegionDO> add(@RequestBody RegionDO domain) {

        ObjectResponse<RegionDO> response = new ObjectResponse<>();

        ResultDO result = RegionService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("region:edit")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="编辑城市", notes="")
    public @ResponseBody
    ObjectResponse<RegionDO> edit(@RequestBody RegionDO domain) {

        ObjectResponse<RegionDO> response = new ObjectResponse<>();

        ResultDO result = RegionService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("region:get")
    @RequestMapping(value="/get.html", method = RequestMethod.POST)
    @ApiOperation(value="获取城市", notes="")
    public @ResponseBody
    ObjectResponse<RegionDO> get(@RequestBody RegionDO domain) {

        ObjectResponse<RegionDO> response = new ObjectResponse<>();

        ResultDO result = RegionService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((RegionDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("region:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除城市", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody RegionDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = RegionService.remove(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("region:list")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    @ApiOperation(value="查询城市", notes="")
    public @ResponseBody  ObjectResponse<List<RegionDO>> page(@RequestBody RegionQueryDO query) {

        ObjectResponse<List<RegionDO>> response = new ObjectResponse<>();

        ResultDO result = RegionService.find(query);

        if(result.isSuccess()) {
            response.setData((List<RegionDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
}
